package com.ruoyi.system.util;

import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;

import java.util.Arrays;
import java.util.List;

import java.util.stream.Collectors;

public class RedisClusterExample {
    public static void main(String[] args) {
        List<String> nodes = Arrays.asList(
                "121.40.250.100:8001",
                "1.94.42.202:8002",
                "123.249.66.70:8002"
        );

        // 转换节点列表为 RedisURI 列表
        List<RedisURI> redisURIs = nodes.stream()
                .map(node -> {
                    String[] parts = node.split(":");
                    return RedisURI.builder()
                            .withHost(parts[0])
                            .withPort(Integer.parseInt(parts[1]))
                            .build();
                })
                .collect(Collectors.toList());

        // 创建集群客户端
        RedisClusterClient clusterClient = RedisClusterClient.create(redisURIs);

        StatefulRedisClusterConnection<String, String> connect = clusterClient.connect();

        System.out.println("成功连接到Redis集群");

    }
}